package com.lbd.system.controller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.system.bean.Dictionary;
import com.lbd.system.service.DictionaryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * Created by WangYajie on 2016/9/19.
 */
@RestController
@RequestMapping("/dictionary")
public class DictionaryController extends BaseController {

    @Autowired
    DictionaryService dictionaryService;

    @RequestMapping(value = "/all/keys", method = RequestMethod.GET)
    public Result allKeys() {
        List<Dictionary> allKeys = dictionaryService.findDictionaryAllKeys();

        return new Result(ResultState.SUCCESS, "查询成功", dictionaryService.findDictionaryAllKeys());
    }


    @RequestMapping(value = "/{key}", method = RequestMethod.GET)
    public Result dictionaryContent(@PathVariable("key") String key, boolean all) {
        List<Dictionary> dictionaryContent = dictionaryService.findDictionaryContent(key);
        if (all) {
            Dictionary dictionary = new Dictionary();
            dictionary.setName("全部");
            dictionary.setCode("");
            dictionaryContent.add(0, dictionary);
        }
        if (CollectionUtils.isNotEmpty(dictionaryContent)) {
            for (Dictionary dictionary : dictionaryContent) {
                dictionary.setParent(null);
            }
        }
        return new Result(ResultState.SUCCESS, "查询成功", dictionaryContent);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, Dictionary dictionary) {
        Page<Dictionary> dd = dictionaryService.findByPageConditionJpql(page, rows, dictionary);
        return new Result(ResultState.SUCCESS, "查询成功", dd);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(Dictionary dictionary) {
        if (dictionary.getParent() != null && StringUtils.isBlank(dictionary.getParent().getId())) {
            dictionary.setParent(null);
        }
        dictionaryService.save(dictionary);
        return new Result(ResultState.SUCCESS, "添加成功");
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestParam("ids[]") String[] ids) {
        System.out.println(Arrays.toString(ids));
        dictionaryService.del(ids);
        return new Result(ResultState.SUCCESS, "删除成功");

    }


    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", dictionaryService.findById(id));

    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(Dictionary dictionary) {
        dictionaryService.update(dictionary);
        return new Result(ResultState.SUCCESS, "更新成功");
    }


}
